<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="80%" top="5vh">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      v-loading="formLoading"
    >
      <el-tabs v-model="activeTab" type="border-card">
        <!-- 基本信息 -->
        <el-tab-pane label="基本信息" name="basic">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="姓名" prop="teacherName">
                <el-input v-model="formData.teacherName" placeholder="请输入姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="工号（OA）" prop="teacherNo">
                <el-input v-model="formData.teacherNo" placeholder="请输入工号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="职务" prop="position">
                <el-input v-model="formData.position" placeholder="请输入职务" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="性别" prop="gender">
                <el-radio-group v-model="formData.gender">
                  <el-radio :label="1">男</el-radio>
                  <el-radio :label="2">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="年龄" prop="age">
                <el-input-number v-model="formData.age" :min="18" :max="100" placeholder="请输入年龄" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="婚否" prop="maritalStatus">
                <el-radio-group v-model="formData.maritalStatus">
                  <el-radio :label="1">已婚</el-radio>
                  <el-radio :label="0">未婚</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="民族" prop="ethnicity">
                <el-input v-model="formData.ethnicity" placeholder="请输入民族" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="身份证号" prop="idCard">
                <el-input v-model="formData.idCard" placeholder="请输入身份证号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="出生日期" prop="birthDate">
                <el-date-picker
                  v-model="formData.birthDate"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择出生日期"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系方式" prop="contactPhone">
                <el-input v-model="formData.contactPhone" placeholder="请输入联系方式" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>

        <!-- 工作信息 -->
        <el-tab-pane label="工作信息" name="work">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="入职日期" prop="entryDate">
                <el-date-picker
                  v-model="formData.entryDate"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择入职日期"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="已满年限" prop="serviceYears">
                <el-input-number v-model="formData.serviceYears" :precision="1" :min="0" placeholder="请输入已满年限" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="签合同次数" prop="contractCount">
                <el-input-number v-model="formData.contractCount" :min="0" placeholder="请输入签合同次数" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="签订合同" prop="contractSignDate">
                <el-date-picker
                  v-model="formData.contractSignDate"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择签订合同日期"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="合同到期" prop="contractExpireDate">
                <el-date-picker
                  v-model="formData.contractExpireDate"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择合同到期日期"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="职业等级" prop="professionalLevel">
                <el-input v-model="formData.professionalLevel" placeholder="请输入职业等级" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="职称" prop="professionalTitle">
                <el-input v-model="formData.professionalTitle" placeholder="请输入职称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="政治面貌" prop="politicalStatus">
                <el-input v-model="formData.politicalStatus" placeholder="请输入政治面貌" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>

        <!-- 教育背景 -->
        <el-tab-pane label="教育背景" name="education">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="学历" prop="educationLevel">
                <el-input v-model="formData.educationLevel" placeholder="请输入学历" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="专业" prop="major">
                <el-input v-model="formData.major" placeholder="请输入专业" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="毕业学校" prop="graduateSchool">
                <el-input v-model="formData.graduateSchool" placeholder="请输入毕业学校" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="毕业时间" prop="graduationDate">
                <el-date-picker
                  v-model="formData.graduationDate"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择毕业时间"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="专业证书" prop="professionalCertificate">
                <el-input
                  v-model="formData.professionalCertificate"
                  type="textarea"
                  :rows="2"
                  placeholder="请输入专业证书"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="教资证书编码" prop="teachingCertificateNo">
                <el-input v-model="formData.teachingCertificateNo" placeholder="请输入教资证书编码" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="颁发单位" prop="certificateIssuer">
                <el-input v-model="formData.certificateIssuer" placeholder="请输入颁发单位" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="颁发日期" prop="certificateIssueDate">
                <el-date-picker
                  v-model="formData.certificateIssueDate"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择颁发日期"
                  style="width: 100%"
                  :disabled="isReadonly"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { TeacherApi, Teacher } from '@/api/ao/teacher'

/** 教师信息 表单 */
defineOptions({ name: 'TeacherForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const activeTab = ref('basic') // 当前激活的标签页

const formData = ref({
  teacherId: undefined,
  deptName: undefined,
  teacherName: undefined,
  teacherNo: undefined,
  position: undefined,
  gender: undefined,
  age: undefined,
  maritalStatus: undefined,
  ethnicity: undefined,
  idCard: undefined,
  birthDate: undefined,
  contactPhone: undefined,
  entryDate: undefined,
  entryYear: undefined,
  entryMonth: undefined,
  serviceYears: undefined,
  householdType: undefined,
  nativePlace: undefined,
  householdAddress: undefined,
  currentAddress: undefined,
  educationLevel: undefined,
  major: undefined,
  graduateSchool: undefined,
  professionalCertificate: undefined,
  teachingCertificateNo: undefined,
  certificateIssuer: undefined,
  certificateIssueDate: undefined,
  contractCount: 0,
  contractSignDate: undefined,
  contractExpireDate: undefined,
  socialInsurance: undefined,
  emergencyContact: undefined,
  personalEmail: undefined,
  qqNumber: undefined,
  politicalStatus: undefined,
  personnelChanges: undefined,
  hobbiesSpecialties: undefined,
  graduationDate: undefined,
  professionalLevel: undefined,
  professionalTitle: undefined,
})

const formRules = reactive({
  teacherName: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
  teacherNo: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await TeacherApi.getTeacher(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as Teacher
    if (formType.value === 'create') {
      await TeacherApi.createTeacher(data)
      message.success(t('common.createSuccess'))
    } else {
      await TeacherApi.updateTeacher(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  activeTab.value = 'basic'
  formData.value = {
    teacherId: undefined,
    deptName: undefined,
    teacherName: undefined,
    teacherNo: undefined,
    position: undefined,
    gender: undefined,
    age: undefined,
    maritalStatus: undefined,
    ethnicity: undefined,
    idCard: undefined,
    birthDate: undefined,
    contactPhone: undefined,
    entryDate: undefined,
    entryYear: undefined,
    entryMonth: undefined,
    serviceYears: undefined,
    householdType: undefined,
    nativePlace: undefined,
    householdAddress: undefined,
    currentAddress: undefined,
    educationLevel: undefined,
    major: undefined,
    graduateSchool: undefined,
    professionalCertificate: undefined,
    teachingCertificateNo: undefined,
    certificateIssuer: undefined,
    certificateIssueDate: undefined,
    contractCount: 0,
    contractSignDate: undefined,
    contractExpireDate: undefined,
    socialInsurance: undefined,
    emergencyContact: undefined,
    personalEmail: undefined,
    qqNumber: undefined,
    politicalStatus: undefined,
    personnelChanges: undefined,
    hobbiesSpecialties: undefined,
    graduationDate: undefined,
    professionalLevel: undefined,
    professionalTitle: undefined,
  }
  formRef.value?.resetFields()
}
</script>
